<script type="text/javascript" src="/bootstrap_select/bootstrap-select.js"></script>
<link rel="stylesheet" type="text/css" href="/bootstrap_select/bootstrap-select.css">
<style>
    .table>thead>tr>th{background: #ddd;}
    .table>tbody>tr>td ,.table>thead>tr>th{padding:8px;line-height:2;vertical-align:bottom;border-bottom:1px solid #ddd;}
    .col-md-12{background:#ffffff;}
    .form-inline>button{height: 40px;width: 100px;font-size: 16px;}
    .form-inline .form-group>input{border-radius:3px;}
    .modal-body .form-group>label{margin-left: 20px;float:left;margin-right: 10px;padding-top: 8px;width: 150px;}
    .modal-body .form-group>input{width: 65%;}
    .form-time {height:34px;border:1px solid #d2d6de;font-size:14px;background-color:#fff;padding:6px 12px;outline:none;}
</style>
<body>
<form class="form-inline" role="form" style="margin-top: 20px;margin-bottom: 20px;">
    <div class="form-group" style="width: 40%;">
        <label class="sr-only" for="name">名称</label>
        <input type="text" class="form-control" id="name" value="<?php if(!empty($_GET['name'])){echo $_GET['name'];}?>" placeholder="请输入姓名" style="width: 300px;height: 40px;">
        <select class="form-control" style="height: 40px;width: 200px;color:#999999;border-radius:3px;" id="dm_type">
            <option value="-1">请选择糖尿病类型</option>
            <option value="1" <?php if(isset($_GET['dm_type'])&&$_GET['dm_type']==1){echo 'selected';} ?>>1型糖尿病</option>
            <option value="2" <?php if(isset($_GET['dm_type'])&&$_GET['dm_type']==2){echo 'selected';} ?>>2型糖尿病</option>
            <option value="3" <?php if(isset($_GET['dm_type'])&&$_GET['dm_type']==3){echo 'selected';} ?>>妊娠型糖尿病</option>
            <option value="4" <?php if(isset($_GET['dm_type'])&&$_GET['dm_type']==4){echo 'selected';} ?>>糖尿病前期</option>
            <option value="5" <?php if(isset($_GET['dm_type'])&&$_GET['dm_type']==5){echo 'selected';} ?>>特殊型糖尿病</option>
            <option value="0" <?php if(isset($_GET['dm_type'])&&$_GET['dm_type']==0){echo 'selected';} ?>>其它</option>
        </select>
    </div>
    <button type="button" class="btn btn-primary" id="select" style="margin-left: 10%;">查询</button>
    <button type="button" class="btn btn-default" id="reset" style="margin-left: 20px;">重置</button>
</form>

<table class="table table-hover">
    <thead>
    <tr>
        <th>姓名</th>
        <th>性别</th>
        <th>年龄</th>
        <th>糖尿病类型</th>
        <th>糖龄</th>
        <th>注册手机号</th>
        <th>注册微信号</th>
        <th>注册来源</th>
        <th>注册时间</th>
        <th>是否认证身份</th>
        <th>绑定设备SN号</th>
        <th>绑定时间</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    @foreach ($result as $item)
        <tr>
            <td>{{$item->name}}</td>
            <td>
                @if($item->gender == 1)
                    男
                @elseif($item->gender == 2)
                    女
                @else
                    保密
                @endif
            </td>
            <td>
                @if(!empty($item->birth_at))
                    {{round(getMonthNum($item->birth_at,date('Y-m-d H:i:s',time()),true / 12))}}
                @endif
            </td>
            <td>
                @if(!empty($item->dm_type))
                    @switch($item->dm_type)
                        @case(0)
                            其它
                            @break
                        @case(1)
                            1型糖尿病
                            @break
                        @case(2)
                            2型糖尿病
                            @break
                        @case(3)
                            妊娠型糖尿病
                            @break
                        @case(4)
                            糖尿病前期
                            @break
                        @case(5)
                            特殊型糖尿病
                            @break
                        @default
                        @break
                    @endswitch
                @endif
            </td>
            <td>
                @if(!empty($item->dm_age))
                    {{getMonthNum($item->dm_age,date('Y-m-d H:i:s',time()))}}
                @endif
            </td>
            <td>{{$item->phone}}</td>
            <td>{{$item->nickname}}</td>
            <td>
                @if($item->from_type == 'APP_IOS')
                    苹果
                @elseif($item->from_type == 'APP_ANDRIOD')
                    安卓
                @else
                    小程序
                @endif
            </td>
            <td>{{$item->created_at}}</td>
            <td>
                @if($item->is_auth == 1)
                    是
                @else
                    否
                @endif
            </td>
            {{--设备编号--}}
            <?php $codeStr = '';$dataTime='';?>
            @foreach ($device as $value)
                @if($value->bundled_user_id == $item->id)
                <?php
                    $codeStr .= $value->device_code.',';
                    $dataTime = $value->bundled_date;
                ?>
                @endif
            @endforeach
            <td><?php echo empty(trim($codeStr,',')) ? '' : trim($codeStr,',');?></td>
            <td><?php echo empty($dataTime) ? '' : $dataTime;?></td>
            <td>
                <a href="details?id={{$item->id}}"><button type="button" data_id = "{{$item->id}}" class="btn btn-info btn_save">患者档案</button></a>&nbsp;&nbsp;
                <button type="button" data_id = "{{$item->id}}" class="btn btn-success btn_bound">设备绑定</button>
            </td>
        </tr>
    @endforeach
    </tbody>
</table>
{{ $result->links() }}


<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content" style="border-radius: 10px;margin-top:30%;">
            <div class="modal-header" style="background: #3c8dbc;color: #fff;border-top-left-radius:10px;border-top-right-radius: 10px;">
                <h4 class="modal-title" id="myModalLabel">新增</h4>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <label for="txt_departmentname">选择设备SN号：</label>
                    <select id="id_select" class="selectpicker bla bla bli" multiple data-live-search="true" style="width: 300px;">
                        {{--<option>cow</option>--}}
                        {{--<option>bull</option>--}}
                        {{--<option selected>ASD</option>--}}
                        {{--<option selected>Bla</option>--}}
                        {{--<option>Ble</option>--}}
                        {{--<option>Ble</option>--}}
                        {{--<option>Ble</option>--}}
                        {{--<option>Ble</option>--}}
                        {{--<option>Ble</option>--}}
                    </select>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
                <button type="button" id="btn_submit" class="btn btn-primary" data-dismiss="modal"  style="margin-left: 20px;">确 定</button>
            </div>
        </div>
    </div>
</div>

</body>

<script>
    $('#reset').click(function () {
        $('#name').val('');
        $('#dm_type').val('-1');
    });

    //查询按钮
    $('#select').click(function () {
        var name = $('#name').val();
        var dm_type = $('#dm_type').val();
        $url = '/admin/user/index?';
        if ( dm_type >= 0 ) {
            $url += 'dm_type='+dm_type+'&';
        }
        if ( name ) {
            $url += 'name='+name;
        }
        window.location.href = $url;
    });


    //绑定设备
    $(".btn_bound").click(function () {
        // 获取未绑定设备
        $.ajax({
            url : '/admin/user/device',
            type: 'get',
            dataType: 'json',
            data: {},
            success:function(result){
                var data = result.data;
                var html = '';
                for ( var i = 0; i < data.length; i++ ) {
                    html += '<option value="'+ data[i].device_code +'">'+ data[i].device_code + ' ' + data[i].brand_name +'</option>';
                }
                $('#id_select').html(html);
                $('.selectpicker').selectpicker('refresh');
                $('.selectpicker').selectpicker('show');
            }
        });
        $('#btn_submit').attr('data_id',$(this).attr('data_id')); //给确定按钮设置用户id属性
        $("#myModalLabel").text("设备绑定");
        $('#myModal').modal();
    });

    //下拉多选搜索框
    $(window).on('load', function () {
        $('.selectpicker').selectpicker({
            'selectedText': 'cat'
        });
    });

    //绑定设备确定按钮
    $('#btn_submit').click(function () {
        var id = $(this).attr('data_id');
        var deviceCode = $('#id_select').val();
        if ( deviceCode ) {
            //绑定设备
            $.ajax({
                url : '/admin/user/bundled',
                type: 'get',
                dataType: 'json',
                data: {code_arr:deviceCode,id:id},
                success:function(result){
                    if ( result.code != 0 ) {
                        alert('操作失败');
                    }else{
                        window.location.reload();
                    }
                }
            });
        }
    });
</script>

